package com.lxxie.common.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by xielx on 21/1/22 14:55
 */
public class MD5Util {
        private MD5Util() {
        }

        public static String MD5(String input) {
                if(input == null || input.length() == 0) {
                        return null;
                }
                try {
                        MessageDigest md5 = MessageDigest.getInstance("MD5");
                        md5.update(input.getBytes());
                        byte[] byteArray = md5.digest();

                        StringBuilder sb = new StringBuilder();
                        for (byte b : byteArray) {
                                // 一个byte格式化成两位的16进制，不足两位高位补零
                                sb.append(String.format("%02x", b));
                        }
                        return sb.toString();
                } catch (NoSuchAlgorithmException e) {
                        e.printStackTrace();
                }
                return null;
        }
}
